package com.tth.learn.addproperties.java;

/**
 * 单例模式之线程安全的懒汉式
 *
 * @Author tth
 * @Date 2022/10/31 17:21
 */
public class BankTest {
}

class Bank {
    private static Bank instance = null;

    // 私有化构造器
    private Bank() {
    }

    public static Bank getInstance() {
        // 方式一：效率稍差
//        synchronized (Bank.class) {
//            if (null == instance) {
//                instance = new Bank();
//            }
//        }

        // 方式二：效率较高
        if (null == instance) {
            synchronized (Bank.class) {
                if (null == instance) {
                    instance = new Bank();
                }
            }
        }

        return instance;
    }
}
